package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.file.FileTypeUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.utils.DictUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysDictData;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.utils.TencentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MaterialInfoMapper;
import com.ruoyi.system.api.domain.MaterialInfo;
import com.ruoyi.system.service.IMaterialInfoService;

/**
 * 资料信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-04
 */
@Service
public class MaterialInfoServiceImpl implements IMaterialInfoService
{
    @Autowired
    private MaterialInfoMapper materialInfoMapper;
    @Autowired
    private RedisService redisService;

    /**
     * 查询资料信息
     * 
     * @param id 资料信息主键
     * @return 资料信息
     */
    @Override
    public MaterialInfo selectMaterialInfoById(Long id)
    {
        List<SysDictData> dictGList = DictUtils.getDictCache("grade");
        List<SysDictData> dictSList = DictUtils.getDictCache("subject");
        List<SysDictData> dictMList = DictUtils.getDictCache("semester");
        HashMap<String, String> dictGMap = new HashMap<>();
        HashMap<String, String> dictSMap = new HashMap<>();
        HashMap<String, String> dictMMap = new HashMap<>();
        for (SysDictData data:dictGList) {
            dictGMap.put(data.getDictValue(),data.getDictLabel());
        }
        for (SysDictData data:dictSList) {
            dictSMap.put(data.getDictValue(),data.getDictLabel());
        }
        for (SysDictData data:dictMList) {
            dictMMap.put(data.getDictValue(),data.getDictLabel());
        }
        MaterialInfo info = materialInfoMapper.selectMaterialInfoById(id);
        info.setGradeName(dictGMap.get(info.getGrade()+""));
        info.setSubjectName(dictSMap.get(info.getSubject()+""));
        info.setSemesterName(dictMMap.get(info.getSemester()+""));
        return info;
    }

    @Override
    public MaterialInfo selectMaterialInfoBySize(Long size) {
        MaterialInfo info = materialInfoMapper.selectMaterialInfoBySize(size);
        return info;
    }

    /**
     * 查询资料信息列表
     * 
     * @param materialInfo 资料信息
     * @return 资料信息
     */
    @Override
    public List<MaterialInfo> selectMaterialInfoList(MaterialInfo materialInfo)
    {
        List<SysDictData> dictGList = DictUtils.getDictCache("grade");
        List<SysDictData> dictSList = DictUtils.getDictCache("subject");
        List<SysDictData> dictMList = DictUtils.getDictCache("semester");
        HashMap<String, String> dictGMap = new HashMap<>();
        HashMap<String, String> dictSMap = new HashMap<>();
        HashMap<String, String> dictMMap = new HashMap<>();
        for (SysDictData data:dictGList) {
            dictGMap.put(data.getDictValue(),data.getDictLabel());
        }
        for (SysDictData data:dictSList) {
            dictSMap.put(data.getDictValue(),data.getDictLabel());
        }
        for (SysDictData data:dictMList) {
            dictMMap.put(data.getDictValue(),data.getDictLabel());
        }
        List<MaterialInfo> list = materialInfoMapper.selectMaterialInfoList(materialInfo);
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        //TencentUtils tencentUtils = TencentUtils.getInstance();
        for (MaterialInfo obj:list) {
            obj.setGradeName(dictGMap.get(obj.getGrade()+""));
            obj.setSubjectName(dictSMap.get(obj.getSubject()+""));
            obj.setSemesterName(dictMMap.get(obj.getSemester()+""));
            if (!SysUser.isAdmin(user.getUserId())) {
                obj.setNoAdmin("1");
            }
            /*if (obj.getMaterialSize()==null) {
                if (null != obj.getUrl() && obj.getUrl().indexOf("aliyuncs.com/") > 0) {
                    String[] keyAry = obj.getUrl().split("aliyuncs.com/");
                    obj.setMaterialSize(tencentUtils.getObjectSize(keyAry[1]));
                }
            }*/
        }
        return list;
    }

    /**
     * 新增资料信息
     * 
     * @param materialInfo 资料信息
     * @return 结果
     */
    @Override
    public int insertMaterialInfo(MaterialInfo materialInfo)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        materialInfo.setCreateTime(DateUtils.getNowDate());
        materialInfo.setUserId(user.getUserId());
        redisService.delCacheMap("MATE:DP");
        return materialInfoMapper.insertMaterialInfo(materialInfo);
    }

    /**
     * 修改资料信息
     * 
     * @param materialInfo 资料信息
     * @return 结果
     */
    @Override
    public int updateMaterialInfo(MaterialInfo materialInfo)
    {
        materialInfo.setCreateTime(DateUtils.getNowDate());
        redisService.delCacheMap("MATE:DP");
        return materialInfoMapper.updateMaterialInfo(materialInfo);
    }

    /**
     * 批量删除资料信息
     * 
     * @param ids 需要删除的资料信息主键
     * @return 结果
     */
    @Override
    public int deleteMaterialInfoByIds(Long[] ids)
    {
        TencentUtils tencentUtils = TencentUtils.getInstance();
        for (Long id:ids) {
            MaterialInfo info = materialInfoMapper.selectMaterialInfoById(id);
            if (null != info.getUrl() && info.getUrl().indexOf("aliyuncs.com/") > 0) {
                String[] keyAry = info.getUrl().split("aliyuncs.com/");
                tencentUtils.del(keyAry[1]);
            }
        }
        redisService.delCacheMap("MATE:DP");
        return materialInfoMapper.deleteMaterialInfoByIds(ids);
    }

    /**
     * 删除资料信息信息
     * 
     * @param id 资料信息主键
     * @return 结果
     */
    @Override
    public int deleteMaterialInfoById(Long id)
    {
        TencentUtils tencentUtils = TencentUtils.getInstance();
        MaterialInfo info = materialInfoMapper.selectMaterialInfoById(id);
        if (null != info.getUrl() && info.getUrl().indexOf("aliyuncs.com/") > 0) {
            String[] keyAry = info.getUrl().split("aliyuncs.com/");
            tencentUtils.del(keyAry[1]);
        }
        redisService.delCacheMap("MATE:DP");
        return materialInfoMapper.deleteMaterialInfoById(id);
    }
}
